/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.Attribute;
import com.kaaction.modules.mer.service.AttributeService;

/**
 * 属性Controller
 * @author huanghaiming
 * @version 2017-05-18
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/attribute")
public class AttributeController extends BaseController {

	@Autowired
	private AttributeService attributeService;
	
	@ModelAttribute
	public Attribute get(@RequestParam(required=false) String classifyId) {
		Attribute entity = null;
		if (StringUtils.isNotBlank(classifyId)){
			entity = attributeService.get(classifyId);
		}
		if (entity == null){
			entity = new Attribute();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:attribute:view")
	@RequestMapping(value = {"list", ""})
	public String list(Attribute attribute, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Attribute> page = attributeService.findPage(new Page<Attribute>(request, response), attribute); 
		model.addAttribute("page", page);
		return "modules/mer/attributeList";
	}

	@RequiresPermissions("mer:attribute:view")
	@RequestMapping(value = "form")
	public String form(Attribute attribute, Model model) {
		model.addAttribute("attribute", attribute);
		return "modules/mer/attributeForm";
	}

	@RequiresPermissions("mer:attribute:edit")
	@RequestMapping(value = "save")
	public String save(Attribute attribute, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, attribute)){
			return form(attribute, model);
		}
		attributeService.save(attribute);
		addMessage(redirectAttributes, "保存属性成功");
		return "redirect:"+Global.getAdminPath()+"/mer/attribute/?repage";
	}
	
	@RequiresPermissions("mer:attribute:edit")
	@RequestMapping(value = "delete")
	public String delete(Attribute attribute, RedirectAttributes redirectAttributes) {
		attributeService.delete(attribute);
		addMessage(redirectAttributes, "删除属性成功");
		return "redirect:"+Global.getAdminPath()+"/mer/attribute/?repage";
	}
	
	@RequiresPermissions("mer:attribute:edit")
	@RequestMapping(value = "deleteAttr",method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Map<String,Object> deleteAttr(Attribute attribute, RedirectAttributes redirectAttributes) {
		return attributeService.deleteAttr(attribute);
	}

}